package org.springblade.modules.desk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.desk.entity.Notice;
import org.springblade.modules.desk.service.INoticeService;
import org.springblade.modules.desk.vo.NoticeUserModel;
import org.springblade.modules.desk.vo.NoticeVO;
import org.springblade.modules.desk.wrapper.NoticeWrapper;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author harry
 * @公众号 Harry技术
 */
@Slf4j
@RestController
@RequestMapping("/api/notice")
@AllArgsConstructor
@ApiSort(2)
@Tag(name = "消息管理", description = "消息")
public class ApiNoticeController {

	private INoticeService noticeService;


	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入notice")
	public R<IPage<NoticeVO>> list(NoticeVO notice, Query query) {
		BladeUser user = SecureUtil.getUser();
		if (user == null) {
			return R.fail("请求异常，请重新登录");
		}
		Long userId = user.getUserId();
		String role = user.getRoleName();
		String tenantId = user.getTenantId();
		log.info("当前登录用户ID:{},role:{},tenantId:{}", userId, role, tenantId);
		// 是否管理员 管理员可以查询所有发布数据
		boolean isAdmin = "admin".equals(role);
		IPage<NoticeVO> pages = noticeService.selectNoticePage(Condition.getPage(query), notice, userId, isAdmin);
		return R.data(pages);
	}

	@ApiLog(value = "新增或修改消息通知")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "新增或修改", description = "传入notice")
	public R submit(@RequestBody Notice notice) {
		return R.status(noticeService.saveOrUpdate(notice));
	}

	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入notice")
	public R<NoticeVO> detail(Notice notice) {
		Notice detail = noticeService.getOne(Condition.getQueryWrapper(notice));
		// 将其设置为已读用户
		BladeUser user = SecureUtil.getUser();
		if (user != null && detail != null) {
			Long userId = user.getUserId();
			String name = user.getUserName();
			List<NoticeUserModel> readUsers = noticeService.updateReadUsers(userId, name, detail);
			detail.setReadUsers(readUsers);
		}
		return R.data(NoticeWrapper.build().entityVO(detail));
	}

	@GetMapping("/unreadMessage")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "未读消息", description = "传入notice")
	public R<List<NoticeVO>> unreadMessage(NoticeVO notice, Query query) {
		BladeUser user = SecureUtil.getUser();
		if (user == null) {
			return R.fail("请求异常，请重新登录");
		}
		Long userId = user.getUserId();
		String role = user.getRoleName();
		String tenantId = user.getTenantId();
		log.info("当前登录用户ID:{},role:{},tenantId:{}", userId, role, tenantId);
		// 是否管理员 管理员可以查询所有发布数据
		boolean isAdmin = "admin".equals(role);
		List<NoticeVO> records = new ArrayList<>();
		IPage<NoticeVO> pages = noticeService.selectNoticePage(Condition.getPage(query), notice, userId, isAdmin);
		// 过滤未读的消息
		if (!pages.getRecords().isEmpty()) {
			records = pages.getRecords().stream().filter(noticeVO -> noticeVO.getIsRead() == 0).toList();
		}
		return R.data(records);
	}
}
